package com.foo.designpattern.observer;

import java.util.ArrayList;

/**
 * com.foo.designpattern.observer
 * Created with IntelliJ IDEA.
 * Created by jiang on 2016-04-22 17:14.
 *
 * @Description 具体主题：气象站
 */
public class WeatherStation implements Subject {
    String forecastTime = null;
    String forecastMess = null;
    int maxTemperature = 0;
    int minTemperature = 0;
    ArrayList<Observer> personList = null;

    public WeatherStation() {
        this.personList = new ArrayList<Observer>();
    }

    @Override
    public void addObserver(Observer observer) {
        if ((observer != null) && (!personList.contains(observer))) {
            this.personList.add(observer);
        }
    }

    @Override
    public void delObserver(Observer observer) {
        if (observer != null) {
            this.personList.remove(observer);
        }
    }

    /**
     * 通知所有观察者
     */
    @Override
    public void notifyObservers() {
        for (int i = 0; i < this.personList.size(); i++) {
            Observer observer = this.personList.get(i);
            observer.update(this);
        }
    }

    /**
     * 新消息
     *
     * @param forecastTime
     * @param forecastMess
     * @param maxTemperature
     * @param minTemperature
     */
    public void doForecast(String forecastTime, String forecastMess, int maxTemperature, int minTemperature) {
        this.forecastTime = forecastTime;
        this.forecastMess = forecastMess;
        this.maxTemperature = maxTemperature;
        this.minTemperature = minTemperature;
        // 如果有新的消息则通知所有观察者
        this.notifyObservers();
    }

    public String getForecastTime() {
        return forecastTime;
    }

    public String getForecastMess() {
        return forecastMess;
    }

    public int getMaxTemperature() {
        return maxTemperature;
    }

    public int getMinTemperature() {
        return minTemperature;
    }

}
